#include "common.h"
/* 
在带头结点的单链表中的第i个位置之前插入元素e
 */
void insertbefore(linklist &l, int i, int e)
{
    if (i < 1)
    {
        // i小于1时输入非法直接返回
        return;
    }
    lnode *pre = l, *p = l->next, *s;
    int j = 1;
    while (p && j < i)
    {
        pre = p;
        p = p->next;
        j++;
    }
    s = new lnode;
    s->data = e;
    s->next = p;
    pre->next = s;
}

int main(int argc, char const *argv[])
{
    int a[] = {1, 2, 4, 5, 6, 7, 8, 9};
    linklist l;
    makelist(l, a, sizeof(a) / sizeof(int));
    printlist(l);
    insertbefore(l, 2, 0);
    printlist(l);
    insertbefore(l, 3, 0);
    printlist(l);
    insertbefore(l, 9, 0);
    printlist(l);
    return 0;
}
